package com.five.controller;

import com.five.dto.UpdateDto;
import com.five.exception.BusinessException;
import com.five.pojo.Chapter;
import com.five.result.Result;
import com.five.service.IChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chapter")
public class ChapterController {
    @Autowired
    private IChapterService chapterService;

    /**
     * 创建章节
     */
    @PostMapping("/createChapter")
    public Result<String> createChapter(@RequestBody Chapter chapter){
        try {
            // 调用章节服务创建章节
            chapterService.createChapter(chapter);
            return Result.success("success");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("创建章节失败");
        }
    }

    /**
     * 批量上传章节
     */
    @PostMapping("/batch")
     public Result<String> batchAdd(@RequestBody List<Chapter> chapters){
        try {
            // 调用章节服务批量创建章节
            chapterService.batchCreate(chapters);
            return Result.success("success");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("批量创建章节失败");
        }
    }

    /**
     * 更新章节
     */
    @PostMapping("/updateChapter")
    public Result<String> updateChapter(@RequestBody Chapter chapter){
        try {
            // 调用章节服务更新章节
            chapterService.update(chapter);
            return Result.success("success");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新章节失败");
        }
    }

    /**
     * 删除章节
     */
     @PostMapping("/deleteChapter/{chapterId}")
    public Result<String> deleteChapter(@PathVariable("chapterId") Long chapterId){
        try {
            chapterService.delete(chapterId);
            return Result.success("success");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("删除章节失败");
        }
    }

    /**
     * 查询章节列表
     */
    @PostMapping("/list/{novelId}")
    public Result<List<Chapter>> queryChapterList(@PathVariable("novelId") Long novelId){
        try {
            List<Chapter> chapterList = chapterService.queryChapterList(novelId);
            return Result.success(chapterList);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("查询章节列表失败");
        }
    }

    /**
     * 查询章节详情
     */
    @PostMapping("/detail/{chapterId}")
    public Result<Chapter> queryChapterDetail(@PathVariable("chapterId") Long chapterId){
        try {
            Chapter chapter = chapterService.queryById(chapterId);
            return Result.success(chapter);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("查询章节详情失败");
        }
    }

    /**
     * 章节状态修改
     */
     @PostMapping("/updateStatus")
    public Result<String> updateStatus(@RequestBody UpdateDto dto){
        try {
            chapterService.updateStatus(dto);
            return Result.success("success");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新章节状态失败");
        }
    }

}
